package statistics;

import java.io.File;
import java.io.IOException;

import main.Simulator;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import event.Event;

public abstract class Statistics {
	
	String name;
	String title;
	String xLabel;
	String yLabel;
	int n;
	Simulator simulator;
	
	XYSeries[] xySeries;
	JFreeChart chart;
	
	public Statistics(Simulator simulator, String name, String xLabel, String yLabel, int n) {
		this.simulator = simulator;
		this.name = name;
		this.xLabel = xLabel;
		this.yLabel = yLabel;
		this.n = n;
		
		this.xySeries = new XYSeries[n];
		for (int i = 0; i < n; i++)
			this.xySeries[i] = new XYSeries("Sessão " + i);
	}
	
	public abstract void collect(Event event);
	
	public void createChart() {
		XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
		for (int sessionId = 0; sessionId < n; sessionId++) {
			xySeriesCollection.addSeries(xySeries[sessionId]);
		}
		chart = ChartFactory.createXYLineChart(title, xLabel, yLabel, xySeriesCollection, PlotOrientation.VERTICAL, true, false, false);
	}
	
	protected void saveAsImage(String name) {
		String filename = name.replace(" ", "") + ".png";
		String dir = simulator.getProperties().getDirectoryToSave();
		File file = dir != null ? new File(new File(dir), filename) : new File(filename);
		try {
			ChartUtilities.saveChartAsPNG(file, chart, 800, 600);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public abstract void saveAsFile();
	
}
